package com.greenantmall.adapter;

import java.text.DecimalFormat;
import java.util.List;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.AntHomeApplication;
import com.greenantmall.activity.DetailActivity;
import com.greenantmall.activity.MyFavActivity;
import com.greenantmall.bean.Goods;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.view.CommonDialog;
import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyFavListAdapter extends BaseAdapter {

	private List<Goods> list;
	private Context context;

	// BitmapUtils bitmapUtils;

	public MyFavListAdapter(List<Goods> list, Context ctx) {
		this.list = list;
		this.context = ctx;
		// bitmapUtils = new
		// BitmapUtils(ctx,ctx.getCacheDir().getAbsolutePath());
		// bitmapUtils.configDiskCacheEnabled(true);
		// bitmapUtils.configMemoryCacheEnabled(true);
		// bitmapUtils.configDefaultLoadingImage(R.drawable.pictures_no);
		// bitmapUtils.configThreadPoolSize(3);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.myfavitem, null);
			viewHolder = new ViewHolder();

			viewHolder.name = (TextView) convertView.findViewById(R.id.name);
			viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
			viewHolder.price = (TextView) convertView
					.findViewById(R.id.iv_googds_price);
			viewHolder.discount = (TextView) convertView
					.findViewById(R.id.iv_googds_discount);
			viewHolder.iconStatus = (ImageView) convertView
					.findViewById(R.id.iconStatus);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final Goods goods = list.get(position);
		final String name = goods.getDescriptionAndSpecs();
		viewHolder.name.setText(name);
		ImageLoader.getInstance().displayImage(goods.getImageUrl(),
				viewHolder.icon, AntHomeApplication.imageDisplayOptions);
		String price = "￥" + goods.getPrice();
		if (goods.getIsOnShelf()) {
			viewHolder.iconStatus.setBackgroundResource(0);
		} else {
			viewHolder.iconStatus.setBackgroundResource(R.drawable.off_shelf);
		}
		double discount = goods.getDiscount();
		viewHolder.discount.getPaint().setFlags(
				Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 中划线

		if (discount <= 0) {
			String strs = price;
			SpannableStringBuilder style = new SpannableStringBuilder(strs);
			style.setSpan(new ForegroundColorSpan(Color.RED), 0,
					price.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			viewHolder.price.setText(style);
		} else {
			DecimalFormat df = new DecimalFormat("#########.##");
			price = "￥"
					+ df.format((goods.getPrice() * goods.getDiscount() / 10));
			String strs = price;
			SpannableStringBuilder style = new SpannableStringBuilder(strs);
			style.setSpan(new ForegroundColorSpan(Color.RED), 0,
					price.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			viewHolder.price.setText(style);

			viewHolder.discount.setText("￥" + goods.getPrice());
		}

		convertView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (goods.getIsOnShelf())
				{
					Intent intent = new Intent(context, DetailActivity.class);
					intent.putExtra(DetailActivity.KEY_DETAIL_OBJECTID,
							goods.getObjectId());
					context.startActivity(intent);
				}else{
					UtilTools.showMyToast(context, context.getString(R.string.off_shelf_prompt_1));
				}
				
			}
		});

		convertView.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				CommonDialog errDialog = new CommonDialog(context,
						R.style.ExitDialogStyle, context
								.getString(R.string.common_title),
						String.format(context
								.getString(R.string.goods_detail_fav_cancel),
								name));
				errDialog
						.setSureListener(new CommonDialog.OnSureClickListener() {

							@Override
							public void sureClicked() {
								RetrieveDataAsyn.getInstance().cancelFav(
										goods.getObjectId(),
										AVUser.getCurrentUser().getObjectId(),
										0, new RetrieveDataListener() {

											@Override
											public void dataRetrieved(
													int requestId, Object data) {
												if (data == null) {
													UtilTools.showMyToast(
															context,
															context.getString(R.string.goods_detail_fav_cancelfailed));
												} else {

													UtilTools.showMyToast(
															context,
															context.getString(R.string.goods_detail_fav_cancelsuccess));
													list.remove(position);
													notifyDataSetChanged();
													if (list.size() == 0) {
														((MyFavActivity) context)
																.refreshView();
													}
												}
											}
										});
							}
						});
				errDialog.show();

				return true;
			}
		});

		return convertView;
	}

	class ViewHolder {

		public TextView name;
		public ImageView icon;
		public ImageView iconStatus;
		public TextView price;
		public TextView discount;

	}

}
